package com.heima.search.service.impl;

import java.util.Date;

import com.heima.common.exception.CustException;
import com.heima.feigns.BehaviorFeign;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDTO;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.service.ApUserSearchService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;

/**
 * @Author XHui
 * @Since 2024/3/16 21:57
 * @Version 1.0
 */
@Service
public class ApUserSearchServiceImpl implements ApUserSearchService {


    @Resource
    private BehaviorFeign behaviorFeign;

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    @Async("taskExecutor")
    public void insert(UserSearchDTO userSearchDto) {
        int fromIndex = userSearchDto.getFromIndex();
        Integer equipmentId = userSearchDto.getEquipmentId();
        String searchWords = userSearchDto.getSearchWords();
        int pageNum = userSearchDto.getPageNum();
        int pageSize = userSearchDto.getPageSize();
        Integer entryId = userSearchDto.getEntryId();
        Date minBehotTime = userSearchDto.getMinBehotTime();

        // 1. 判断用户是否已登录
        ApUser currentUser = AppThreadLocalUtils.getUser();

        // 2. 根据userId或设备ID查询是否存在对应行为实体
        ResponseResult<ApBehaviorEntry> responseResult = behaviorFeign.findByUserIdOrEquipmentId(entryId, equipmentId);
        if (!responseResult.checkCode()) {
            CustException.cust(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
        }
        ApBehaviorEntry behaviorEntryDB = responseResult.getData();

        // 3. 行为实体不存在，新增行为实体
        if (behaviorEntryDB == null) {
            behaviorEntryDB = new ApBehaviorEntry();
            behaviorEntryDB.setCreatedTime(new Date());

            if (currentUser != null) {
                behaviorEntryDB.setRefId(currentUser.getId());
                behaviorEntryDB.setType(ApBehaviorEntry.Type.USER.getCode());
            } else {
                behaviorEntryDB.setRefId(equipmentId);
                behaviorEntryDB.setType(ApBehaviorEntry.Type.EQUIPMENT.getCode());
            }
            mongoTemplate.save(behaviorEntryDB);
        }

        // 4. 行为存在， 查询当前用户搜索的关键词是否存在
        ApUserSearch apUserSearchDB = mongoTemplate.findOne(new Query(
                Criteria.where("entryId").is(entryId)
                        .and("keyword").is(searchWords)
        ), ApUserSearch.class);
        if(apUserSearchDB != null){
            apUserSearchDB.setCreatedTime(new Date());
        }else {
            apUserSearchDB = new ApUserSearch();
            apUserSearchDB.setKeyword(searchWords);
            apUserSearchDB.setCreatedTime(new Date());
            apUserSearchDB.setEntryId(behaviorEntryDB.getId());
        }
        mongoTemplate.save(apUserSearchDB);
        // 6. 新增搜索历史

        // 7
    }


}
